# -*- coding: utf-8 -*-

import unittest
import os.path

from kivy.lang import Builder

from presenter import Presenter
from model.gamestate import *
from ui.welcome_screen import WelcomeScreen
from main import MainView


class PresenterTest(unittest.TestCase):
    def setUp(self):
        Builder.load_file(os.path.join(os.path.dirname(__file__), '..',
                                       'mainview.kv'))
        self.presenter = Presenter(None, './data/config.ini')
        self.presenter.register_view(Phase.WELCOME,
                                     WelcomeScreen, ['welcomescreen'])

        players = Players([Player('Andrey', []), Player('Boris', []),
                           Player('Charlie', []), Player('Dmitry', [])])
        players.set_next_pair_strategy(TraditionalStrategy(players.players))
        hat = Hat(['стул', 'шапка', 'кастрюлька'])
        game_round = GameRound(20, [], '', (players.players[0],
                                            players.players[2]))

        self.presenter.game_state = GameState(players, hat,
                                              game_round, 0, Phase.WELCOME,
                                              None, Settings(20, 30))
        self.presenter.main_view = MainView()
        self.presenter.change_phase(Phase.WELCOME)
        self.game_state_before_pause = self.presenter.game_state

    def test_pause_app(self):
        self.assertEquals(self.presenter.game_state,
                          self.game_state_before_pause)
        self.presenter.close_app()
        self.presenter.resume_app()
        self.assertEqual(self.presenter.game_state,
                         self.game_state_before_pause)

        if os.path.exists('data/pickled_gamestate.pkl'):
            os.remove('data/pickled_gamestate.pkl')


if __name__ == "__main__":
    unittest.main()
